//
//  GoodBrandViewController.m
//  danpin
//
//  Created by chuangjia on 29/8/15.
//  Copyright (c) 2015 chuangjia. All rights reserved.
//

#import "GoodBrandViewController.h"

#import "GoodClassViewController.h"
#import "GoodViewController.h"
#import "CellConfig.h"
#import "CommentViewController.h"
#import "BaseModel.h"
#import "ZToastManager.h"

@interface GoodBrandViewController() <UITableViewDataSource,UITableViewDelegate>
{
    
}
@property (nonatomic,strong)NSMutableDictionary *brandDictionary;
@property(nonatomic,strong)NSMutableArray *sectionsArray;
@property(nonatomic,strong)NSMutableArray *datasArray;
@property(nonatomic,strong)NSMutableArray *keysArray;
@property(nonatomic,strong)NSMutableArray *tagsArray;
@property(nonatomic,strong)NSMutableArray *othersArray;
@property(nonatomic,strong)UITableView *tbView;
@property(nonatomic,assign)int currentPage;
@property(nonatomic,strong)NSString* currentBid;
@property(nonatomic,assign)BOOL isLoadFinished;
@end

@implementation GoodBrandViewController

-(void)dealloc
{
    [_sectionsArray removeAllObjects];
    _sectionsArray=nil;
    [_datasArray removeAllObjects];
    _datasArray =nil;
    [_keysArray removeAllObjects];
    _keysArray =nil;
    [_tagsArray removeAllObjects];
    _tagsArray=nil;
    _brandDictionary=nil;
    [_othersArray removeAllObjects];
    _othersArray=nil;
    self.tbView =nil;
    DLog(@"%s", __PRETTY_FUNCTION__);
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [UMengShare event:CK_GOODBRAND acc:[_currentBid integerValue]];
    if (!_sectionsArray) {
        _sectionsArray =[[NSMutableArray alloc] init];
    }
    if (!_datasArray) {
        _datasArray =[[NSMutableArray alloc] init];
    }
    if (!_keysArray) {
        _keysArray=[[NSMutableArray alloc] init];
    }
    if (!_tagsArray) {
        _tagsArray =[[NSMutableArray alloc] init];
    }
    if (!_brandDictionary) {
        _brandDictionary =[NSMutableDictionary dictionary];
    }
    if (!_othersArray) {
        _othersArray =[[NSMutableArray alloc] init];
    }
    
    self.isLoadFinished=NO;
    // Do any additional setup after loading the view.
    self.view.backgroundColor =[UIColor whiteColor];
    self.currentPage =0;
    self.isComment=YES;
    self.navTitle =self.brandName;

    [self notificationManager];
    [self initUI];
   
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.enableBackGesture=YES;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    DLog(@"%s", __PRETTY_FUNCTION__);
}

-(void)initUI
{
    _tbView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.navHeight, self.view.frame.size.width, self.view.frame.size.height-self.navHeight) style:UITableViewStyleGrouped];
    _tbView.delegate = self;
    _tbView.dataSource = self;
    _tbView.backgroundColor = [UIColor whiteColor];
    _tbView.showsVerticalScrollIndicator = NO;
    _tbView.separatorStyle= UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tbView];
    __weak typeof (self) wSelf = self;
    [_tbView addInfiniteScrollingWithActionHandler:^{
        
        if (wSelf.isLoadFinished) {
            //停止菊花
            [wSelf.tbView.infiniteScrollingView stopAnimating];
            [wSelf.tbView setShowsInfiniteScrolling:NO];
            return;
        }
        [wSelf.tbView.infiniteScrollingView startAnimating];
        if ([wSelf.othersArray count]>0) {
            wSelf.currentPage=(int)[wSelf.othersArray count]+1;
        }
        [wSelf getCategoryDataWithBid:wSelf.brandId tag:@""];
    }];
  //  [ZShare showAnimationView:self.view center:CGPointZero];
    [self getCategoryDataWithBid:self.brandId tag:@""];
}
#pragma mark - tableview
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return  [self.sectionsArray count];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section>self.sectionsArray.count) {
        return 0;
    }
    NSString *sectionTitle =[self.sectionsArray objectAtIndex:section];
    if ([sectionTitle isEqualToString:@"brand_detail"]||[sectionTitle isEqualToString:@"brand_tags"]) {
        return 1;
    }else if ([sectionTitle isEqualToString:@"others"]) {
        return [self.othersArray count];
    }else{
        if (section>self.brandDictionary.count) {
            return 0;
        }
        id tag =[self.brandDictionary objectForKey:sectionTitle];
        if(tag && [tag isKindOfClass:[NSDictionary class]])
        {
            NSArray *keyArry =[tag allKeys];
            if ([keyArry containsObject:@"99999999"]) {
                int end = [[NSString stringWithFormat:@"%@",[[tag  objectForKey:@"99999999"] objectForKey:@"end"]] intValue];
                if (end==1) {
                    return [keyArry count]-1;
                }
            }
            return [keyArry count];
        }
    }
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section>self.sectionsArray.count) {
        return 0;
    }
    NSString *sectionTitle =[self.sectionsArray objectAtIndex:indexPath.section];
    if ([sectionTitle isEqualToString:@"brand_detail"]) {
        return [BrandDetailCell returnCellHeight:nil];
    }else if ([sectionTitle isEqualToString:@"brand_tags"]) {
        return [BrandTagCell returnCellHeight:self.tagsArray];
    }else if ([sectionTitle isEqualToString:@"others"]) {
        NSDictionary *dict =[self.othersArray objectAtIndex:indexPath.row];
        return [CategoryListCell returnCellHeight:dict];
    }else{
        if (indexPath.section>self.brandDictionary.count) {
            return 0;
        }
        id tag =[self.brandDictionary objectForKey:sectionTitle];
        if(tag && [tag isKindOfClass:[NSDictionary class]])
        {
            NSMutableArray *numArray = [NSMutableArray arrayWithArray:[tag allKeys]];
            [numArray sortUsingComparator: ^NSComparisonResult (NSString *str1, NSString *str2) {
                NSNumber *number1 = [NSNumber numberWithInteger:str1.floatValue];
                NSNumber *number2 = [NSNumber numberWithInteger:str2.floatValue];
                return [number1 compare:number2];
            }];
            if ([numArray containsObject:@"99999999"]) {
                int end = [[NSString stringWithFormat:@"%@",[[tag  objectForKey:@"99999999"] objectForKey:@"end"]] intValue];
                if (end==1) {
                    [numArray removeObject:@"99999999"];
                }
            }
            NSString* kv =[numArray objectAtIndex:indexPath.row];
            
            if ([kv isEqualToString:@"99999999"]) {
                return 60;
            }else {
                NSDictionary* dict =[tag objectForKey:kv];
                if ([[dict objectForKey:@"landscape"] intValue]==1)
                {
                    return [CategoryV1Cell returnCellHeight:dict];
                }else{
                    return [CategoryListCell returnCellHeight:dict];
                }
            }
        }
        
        return 0;
        
    }
    return 0;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    NSString *sectionTitle =[self.sectionsArray objectAtIndex:section];
    if ([sectionTitle isEqualToString:@"brand_detail"]) {
        return PHOTO_FRAME_WIDTH;
    }else if ([sectionTitle isEqualToString:@"brand_tags"]) {
        return 0.01;
    }else if ([sectionTitle isEqualToString:@"others"]) {
        if (self.brandDictionary && self.brandDictionary.count>0) {
            return PHOTO_FRAME_WIDTH*5;
        }
        return 0.01;
    }else{
        return PHOTO_FRAME_WIDTH *4;
    }
    return 0.01;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return PHOTO_FRAME_WIDTH;
}

-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    NSString *sectionTitle =[self.sectionsArray objectAtIndex:section];
    if ([sectionTitle isEqualToString:@"brand_detail"]||[sectionTitle isEqualToString:@"brand_tags"]){
        return nil;
    }else  if ([sectionTitle isEqualToString:@"others"]) {
        if (self.brandDictionary&&self.brandDictionary.count>0) {
            UIView *sectionView =[[UIView alloc] initWithFrame:CGRectMake(0, 0, _tbView.frame.size.width, PHOTO_FRAME_WIDTH*4)];
            sectionView.backgroundColor =[UIColor clearColor];
            UILabel *tagValue =[[UILabel alloc] initWithFrame:CGRectMake(0,PHOTO_FRAME_WIDTH+2, _tbView.frame.size.width, PHOTO_FRAME_WIDTH*2)];
            tagValue.backgroundColor = [UIColor clearColor];
            tagValue.textColor = NICKNAME_COLOR;
            tagValue.textAlignment = NSTextAlignmentCenter;
            tagValue.font = FONT(20);
            NSString *str =[NSString stringWithFormat:@"「 %@ 」",@"其他"];
            tagValue.text =str;
            [sectionView addSubview:tagValue];
            tagValue=nil;
            return sectionView;
        }
    }else{
        UIView *sectionView =[[UIView alloc] initWithFrame:CGRectMake(0, 0, _tbView.frame.size.width, PHOTO_FRAME_WIDTH *4)];
        sectionView.backgroundColor =[UIColor clearColor];
        UILabel *tagValue =[[UILabel alloc] initWithFrame:CGRectMake(0, PHOTO_FRAME_WIDTH, _tbView.frame.size.width, PHOTO_FRAME_WIDTH *2)];
        tagValue.backgroundColor = [UIColor clearColor];
        tagValue.textColor = NICKNAME_COLOR;
        tagValue.textAlignment = NSTextAlignmentCenter;
        tagValue.font = FONT(20);
        NSString *str =[NSString stringWithFormat:@"「 %@ 」",sectionTitle];
        tagValue.text =str;
        [sectionView addSubview:tagValue];
        tagValue=nil;
        return sectionView;
    }
    
    return nil;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat sectionHeaderHeight = 50;
    if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
        scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
    } else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
        scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
    }
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //   UITableViewCell *cell = (UITableViewCell*)[tableView  dequeueReusableCellWithIdentifier:@""];

    NSString *sectionTitle =[self.sectionsArray objectAtIndex:indexPath.section];
    
    if ([sectionTitle isEqualToString:@"brand_detail"]) {
        BrandDetailCell *cell = (BrandDetailCell*)[tableView  dequeueReusableCellWithIdentifier:@""];
        if(cell == nil)
        {
            cell = [[BrandDetailCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
            cell.backgroundColor=[UIColor clearColor];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        [cell showInfo:nil indexPath:indexPath];
        return cell;
    }else if ([sectionTitle isEqualToString:@"brand_tags"]) {
        BrandTagCell *cell = (BrandTagCell*)[tableView  dequeueReusableCellWithIdentifier:@""];
        if(cell == nil)
        {
            cell = [[BrandTagCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
            cell.backgroundColor=[UIColor clearColor];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            __weak typeof (self) wSelf = self;
            [cell setCallBack:^(BEventType t, UIView *v,id obj,id key,NSIndexPath *indexPath) {
                [wSelf cellAction:t views:v obj:obj key:key indexPath:indexPath];
            }];
        }
        [cell showInfo:_tagsArray indexPath:indexPath];
        return cell;
    }else if ([sectionTitle isEqualToString:@"others"]) {
        
        CategoryListCell *cell = (CategoryListCell*)[tableView  dequeueReusableCellWithIdentifier:@""];
        if(cell == nil)
        {
            cell = [[CategoryListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
            __weak typeof (self) wSelf = self;
            [cell setCallBack:^(BEventType t, UIView *v,id obj,id key,NSIndexPath *indexPath) {
                [wSelf cellAction:t views:v obj:obj key:key indexPath:indexPath];
            }];
        }
        if (indexPath.row >self.othersArray.count) {
            return cell;
        }
        NSDictionary *dict =[self.othersArray objectAtIndex:indexPath.row];
        [cell showInfo:dict key:@"others" indexPath:indexPath];
        return cell;
    }else{
        id tag =[self.brandDictionary objectForKey:sectionTitle];
        if(tag && [tag isKindOfClass:[NSDictionary class]])
        {
            NSMutableArray *numArray = [NSMutableArray arrayWithArray:[tag allKeys]];
            [numArray sortUsingComparator: ^NSComparisonResult (NSString *str1, NSString *str2) {
                NSNumber *number1 = [NSNumber numberWithInteger:str1.floatValue];
                NSNumber *number2 = [NSNumber numberWithInteger:str2.floatValue];
                return [number1 compare:number2];
            }];
            if ([numArray containsObject:@"99999999"]) {
                int end = [[NSString stringWithFormat:@"%@",[[tag  objectForKey:@"99999999"] objectForKey:@"end"]] intValue];
                if (end==1) {
                    [numArray removeObject:@"99999999"];
                }
            }
            NSString* kv =[numArray objectAtIndex:indexPath.row];
            if ([kv isEqualToString:@"99999999"]) {
                UITableViewCell *cell = (UITableViewCell*)[tableView  dequeueReusableCellWithIdentifier:@"99999999"];

                if(cell == nil)
                {
                    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"99999999"];
                    cell.contentView.backgroundColor = [UIColor clearColor];
                    cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
                    cell.selectedBackgroundView.backgroundColor =UIColorFromRGB(0xf4f4f4);
                }
                cell.textLabel.textAlignment =NSTextAlignmentCenter;
                cell.textLabel.text =[NSString stringWithFormat:@"%@",[[tag  objectForKey:@"99999999"] objectForKey:@"end_msg"]];
                cell.textLabel.font = FONT(16);
                cell.textLabel.textColor=UIColorFromRGB(0x285790);
                return cell;
            }else{
                
                NSDictionary* dict =[tag objectForKey:kv];
                if ([[dict objectForKey:@"landscape"] intValue]==1)
                {
                    CategoryV1Cell *cell = (CategoryV1Cell*)[tableView  dequeueReusableCellWithIdentifier:@""];
                    if(cell == nil)
                    {
                        cell = [[CategoryV1Cell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
                        
                        __weak typeof (self) wSelf = self;
                        [cell setCallBack:^(BEventType t, UIView *v,id obj ,id key,NSIndexPath *indexPath) {
                            [wSelf cellAction:t views:v obj:obj key:key indexPath:indexPath];
                        }];
                    }
                    [cell showInfo:dict other:[NSNumber numberWithBool:YES] key:@"brandView" indexPath:indexPath];
                    return cell;
                }else{
                    CategoryListCell *cell = (CategoryListCell*)[tableView  dequeueReusableCellWithIdentifier:@""];
                    if(cell == nil)
                    {
                        cell = [[CategoryListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
                        
                        __weak typeof (self) wSelf = self;
                        [cell setCallBack:^(BEventType t, UIView *v,id obj ,id key,NSIndexPath *indexPath) {
                            [wSelf cellAction:t views:v obj:obj key:key indexPath:indexPath];
                        }];
                    }
                    [cell showInfo:dict key:sectionTitle indexPath:indexPath];
                    return cell;
                }
            }
        }
    }
    
    return [self getErrorcell:tableView];
//    return cell;
}


- (UITableViewCell*)getErrorcell:(UITableView*)tableView
{
    UITableViewCell *cell = (UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:@"ErrorEmptyCell"];
    if(cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ErrorEmptyCell"];
    }
    return  cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSString *sectionTitle =[self.sectionsArray objectAtIndex:indexPath.section];
    
    if ([sectionTitle isEqualToString:@"others"])
    {
        CategoryListCell *cell = (CategoryListCell *)[tableView cellForRowAtIndexPath:indexPath];
        
        GoodViewController *good =[[GoodViewController alloc] init];
        good.dictionary = cell.data;
        good.type =-1;
        good.photo_id = [NSString stringWithFormat:@"%@",[cell.data objectForKey:@"photo_id"]];
        [self.navigationController pushViewController:good animated:YES];
        
    }else if(![sectionTitle isEqualToString:@"brand_tags"]&& ![sectionTitle isEqualToString:@"brand_detail"])
    {
        id tag =[self.brandDictionary objectForKey:sectionTitle];
        if(tag && [tag isKindOfClass:[NSDictionary class]])
        {
            NSMutableArray *numArray = [NSMutableArray arrayWithArray:[tag allKeys]];
            [numArray sortUsingComparator: ^NSComparisonResult (NSString *str1, NSString *str2) {
                NSNumber *number1 = [NSNumber numberWithInteger:str1.floatValue];
                NSNumber *number2 = [NSNumber numberWithInteger:str2.floatValue];
                return [number1 compare:number2];
            }];
            if ([numArray containsObject:@"99999999"]) {
                int end = [[NSString stringWithFormat:@"%@",[[tag  objectForKey:@"99999999"] objectForKey:@"end"]] intValue];
                if (end==1) {
                    [numArray removeObject:@"99999999"];
                }
            }
            NSString* kv =[numArray objectAtIndex:indexPath.row];
            
            if ([kv isEqualToString:@"99999999"])
            {
                NSString *cate =[NSString stringWithFormat:@"%@",[[tag  objectForKey:@"99999999"] objectForKey:@"next"]];
                if ([cate isEqualToString:@"cate"]) {
                    NSString *cateid =[NSString stringWithFormat:@"%@",[[tag  objectForKey:@"99999999"] objectForKey:@"next_id"]];
                    NSString *catename =[NSString stringWithFormat:@"%@",[[tag  objectForKey:@"99999999"] objectForKey:@"next_name"]];
                    GoodClassViewController *cate =[[GoodClassViewController alloc] init];
                    cate.cateName =catename;
                    cate.cateCid =cateid;
                    cate.cateTag =@"";
                    [self.navigationController pushViewController:cate animated:YES];
                }else
                {
                    [self moreWithTag:sectionTitle];
                }
                
            }else{
                NSDictionary* dict =[tag objectForKey:kv];
                
                GoodViewController *good =[[GoodViewController alloc] init];
                good.type =-1;
                good.dictionary = dict;
                good.photo_id = [NSString stringWithFormat:@"%@",[dict objectForKey:@"photo_id"]];
                [self.navigationController pushViewController:good animated:YES];
            }
        }
        
        
        
    }
    
}

-(void)cellAction:(BEventType)type views:(UIView *)v obj:(id)obj key:(id)key indexPath:(NSIndexPath*)indexPath
{
    switch (type) {
        case BEventType_category:
        {
            GoodClassViewController *cate =[[GoodClassViewController alloc] init];
            cate.cateName =[NSString stringWithFormat:@"%@",[obj objectForKey:@"cat2"]];
            cate.cateCid =[NSString stringWithFormat:@"%@",[obj objectForKey:@"cat2_id"]];
            cate.cateTag =@"";
            [self.navigationController pushViewController:cate animated:YES];
        }
            break;
        case BEventType_categoryTag:
        {
            GoodClassViewController *cate =[[GoodClassViewController alloc] init];
            cate.cateCid =[NSString stringWithFormat:@"%@",[obj objectForKey:@"cat2_id"]];
            cate.cateName =[NSString stringWithFormat:@"%@",[obj objectForKey:@"cat2"]];
            cate.cateTag =[NSString stringWithFormat:@"%@",key];
            [self.navigationController pushViewController:cate animated:YES];
        }
            break;
        case BEventType_brand:
        {
            //            BrandViewController *brand =[[BrandViewController alloc] init];
            //            brand.dictionary =obj;
            //            [self.flipboardNavigationController pushViewController:brand];
        }
            break;
        case BEventType_good:
        {
            GoodViewController *good =[[GoodViewController alloc] init];
            good.type =-1;
            good.dictionary =obj;
            good.photo_id = [NSString stringWithFormat:@"%@",[obj objectForKey:@"photo_id"]];
            [self.navigationController pushViewController:good animated:YES];
        }
            break;
        case BEventType_comment:
        {
            NSString *pid =[obj objectForKey:@"photo_id"];
            CommentViewController *comm =[[CommentViewController alloc] init];
            comm.photo_id =pid;
            [self.navigationController pushViewController:comm animated:YES];
        }
            break;
        case BEventType_like:
        {
            if ([self isLogin]) {
                NSString *pid =[obj objectForKey:@"photo_id"];
                [self likeWithPid:pid key:key indexPath:indexPath];
            }
        }
            break;
        case BEventType_tag:
        {
            if ([obj isEqualToString:@"更多"]) {
                return;
            }
            self.currentPage=0;
            [self getCategoryDataWithBid:self.brandId tag:obj];
        }
            break;
        default:
            break;
    }
}
#pragma mark -----
-(void)getCategoryDataWithBid:(NSString *)bid tag:(NSString*)tag
{
    NSMutableDictionary *dict =[NSMutableDictionary dictionary];
    [dict setValue:@"brand" forKey:@"action"];
    [dict setValue:@"100001" forKey:@"guest_id"];
    [dict setValue:@"" forKey:@"user_id"];
    [dict setValue:@"" forKey:@"user_name"];
    [dict setValue:[NSString stringWithFormat:@"%@",self.cId] forKey:@"cate_id"];
    [dict setValue:bid forKey:@"brand_id"];
    [dict setValue:@"" forKey:@"sku_name"];
    [dict setValue:[NSString stringWithFormat:@"%d",self.currentPage] forKey:@"from"];
    [dict setValue:@"1" forKey:@"grid"];
    [dict setValue:@"" forKey:@"lat"];
    [dict setValue:@"" forKey:@"lon"];
    [dict setValue:tag forKey:@"tag"];
    __weak typeof (self) wSelf = self;
    [BaseModel wareList:dict allowFetchCache:NO success:^(AFHTTPRequestOperation *operation, NSObject *result) {
        [wSelf parserResultObject:result];
        [[ZToastManager ShardInstance] hideprogress];
        [wSelf.tbView.infiniteScrollingView stopAnimating];
    //    [ZShare hiddenAnimationView:wSelf.view];
    } failure:^(NSError *error) {
        [[ZToastManager ShardInstance] hideprogress];
        [_tbView.infiniteScrollingView stopAnimating];
    //    [ZShare hiddenAnimationView:wSelf.view text:@""];
    }];
    
}
-(void)parserResultObject:(id)resultObject
{
    
    if (resultObject==nil) {
        return;
    }
    if (resultObject && [resultObject isKindOfClass:[NSDictionary class]]) {
        //
        if (self.currentPage==0) {
            
            self.sectionsArray =[NSMutableArray arrayWithArray:@[@"brand_tags",@"others"]];
            //
            //
            NSMutableArray *sortTempArray =[NSMutableArray array];
            id brand_tags =  [resultObject objectForKey:@"brand_tags"];
            if (brand_tags && [brand_tags isKindOfClass:[NSArray class]]){
                
                if (self.currentPage==0) {
                    [self.tagsArray removeAllObjects];
                }
                for (NSString *di in brand_tags) {
                    [self.tagsArray addObject:di];
                    [sortTempArray addObject:di];
                }
                if ([self.tagsArray count]==0) {
                    [self.sectionsArray removeObject:@"brand_tags"];
                }
            }else
            {
                if (self.currentPage==0) {
                    [self.sectionsArray removeObject:@"brand_tags"];
                }
            }
            //
            id data =[resultObject objectForKey:@"data"];
            if (data && [data isKindOfClass:[NSDictionary class]]) {
                int scount =(int)[sortTempArray count];
                if(scount>0)
                {
                    for (int i =0 ;i<scount ;i++) {
                        NSString *tagName =[sortTempArray objectAtIndex:i];
                        for (NSString *datakey in [data allKeys])
                        {
                            if ([datakey isEqualToString:tagName]) {
                                [self.sectionsArray insertObject:datakey atIndex:[self.sectionsArray count]-1];
                                break;
                            }
                        }
                    }
                }else{
                    for (NSString *datakey in [data allKeys])
                    {
                        [self.sectionsArray insertObject:datakey atIndex:[self.sectionsArray count]-1];
                    }
                }
                [self.brandDictionary addEntriesFromDictionary:data];
            }else{
                [self.brandDictionary removeAllObjects];
            }
            //
            id others =[resultObject objectForKey:@"others"];
            
            if (others && [others isKindOfClass:[NSArray class]]) {
                if (self.currentPage==0) {
                    [self.othersArray removeAllObjects];
                }
                for (NSDictionary *dict in others) {
                    [self.othersArray addObject:dict];
                }
                if ([self.othersArray count]==0) {
                    [self.sectionsArray removeObject:@"others"];
                }
            }else
            {
                if (self.currentPage==0) {
                    [self.sectionsArray removeObject:@"others"];
                }
            }
        }else{
            id others =[resultObject objectForKey:@"others"];
            
            if (others && [others isKindOfClass:[NSArray class]]) {
                for (NSDictionary *dict in others) {
                    [self.othersArray addObject:dict];
                }
            }
        }
        //end
        
        
        self.isLoadFinished =  [[resultObject objectForKey:@"end"] boolValue];
                
        if (self.isLoadFinished) {
            [self.tbView.infiniteScrollingView stopAnimating];
            [self.tbView setShowsInfiniteScrolling:NO];
            NSString *end_msg =[resultObject objectForKey:@"end_msg"];
            self.tbView.tableFooterView =[ZShare footerView:end_msg];
        }
    }
    [_tbView reloadData];
    
}
//喜欢
-(void)likeWithPid:(NSString*)pid key:(NSString*)key indexPath:(NSIndexPath*)indexPath
{
    [UMengShare event:CK_LOVE acc:1];
    __weak typeof (self) wSelf = self;
    NSMutableDictionary *dict =[NSMutableDictionary dictionary];
    [dict setValue:pid forKey:@"photo_id"];
    [BaseModel love:dict success:^(AFHTTPRequestOperation *operation, id result) {
        int code =  [[result objectForKey:@"code"] intValue];
        NSString *like_cnt =  [NSString stringWithFormat:@"%@",[result objectForKey:@"like_cnt"]];
        if (code==1 || code==2)
        {
            if ([key isEqualToString:@"others"]) {
                for (int i=0; i<[wSelf.othersArray count]; i++) {
                    NSDictionary *dict =[wSelf.othersArray objectAtIndex:i];
                    if ([dict isKindOfClass:[NSDictionary class]]) {
                        NSString *pidd =[dict objectForKey:@"photo_id"];
                        if ([pidd isEqualToString:pid]) {
                            NSMutableDictionary *temp = [dict mutableCopy];
                            if (code==1) {
                                [temp setValue:@"1" forKey:@"like"];
                            }else{
                                [temp setValue:@"0" forKey:@"like"];
                            }
                            [temp setValue:like_cnt forKey:@"like_cnt"];
                            [wSelf.othersArray replaceObjectAtIndex:i withObject:temp];
                            break;
                        }
                    }
                }
            }else
            {
                [wSelf.brandDictionary enumerateKeysAndObjectsUsingBlock:^(id key,id value,BOOL *stop){
                    id keyArray = [[wSelf.brandDictionary objectForKey:key] mutableCopy];
                    if(keyArray && [keyArray isKindOfClass:[NSDictionary class]])
                    {
                        NSArray *keyArry =[keyArray allKeys];
                        keyArry= [keyArry sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
                        NSMutableArray *key1 =[NSMutableArray arrayWithArray:keyArry];
                        if ([key1 containsObject:@"99999999"]) {
                            [key1 removeObject:@"99999999"];
                        }
                        
                        for(int i =0 ;i<[key1 count];i++)
                        {
                            NSString *kv =[key1 objectAtIndex:i];
                            NSDictionary *dict =[keyArray objectForKey:kv];
                            NSString *pidd =[dict objectForKey:@"photo_id"];
                            if ([pidd isEqualToString:pid]) {
                                NSMutableDictionary *temp = [dict mutableCopy];
                                if (code==1) {
                                    [temp setValue:@"1" forKey:@"like"];
                                }else{
                                    [temp setValue:@"0" forKey:@"like"];
                                }
                                [temp setValue:like_cnt forKey:@"like_cnt"];
                                [keyArray setValue:temp forKey:kv];
                                
                                break;
                            }
                        }
                    }
                    [self.brandDictionary setValue:keyArray forKey:key];
                }];
                
            }
            
            [[NSNotificationCenter defaultCenter] postNotificationName:@"updateLoveList" object:pid];
            
            [_tbView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationNone];
            
        }
    } failure:^(NSError *error) {
    //    [ZShare hiddenAnimationView:wSelf.view text:@""];
    }];
    
}
//更多
-(void)moreWithTag:(NSString *)tagstr
{
    NSDictionary *dictk =[self.brandDictionary objectForKey:tagstr];
    int from =(int)[[dictk allKeys] count]-1;
    NSMutableDictionary *dict =[NSMutableDictionary dictionary];
    [dict setValue:@"brand" forKey:@"action"];
    [dict setValue:@"" forKey:@"user_id"];
    [dict setValue:self.brandId forKey:@"cate_id"];
    [dict setValue:@"" forKey:@"brand_id"];
    [dict setValue:tagstr forKey:@"tag"];
    [dict setValue:@"" forKey:@"sku_name"];
    [dict setValue:[NSString stringWithFormat:@"%d",from] forKey:@"from"];
    [dict setValue:@"1" forKey:@"grid"];
    [dict setValue:@"" forKey:@"lat"];
    [dict setValue:@"" forKey:@"lon"];
    __weak typeof (self) wSelf = self;
    [BaseModel wareList:dict  allowFetchCache:NO  success:^(AFHTTPRequestOperation *operation, NSObject *result) {
        [wSelf parserMoreObject:result tag:tagstr];
    } failure:^(NSError *error) {
    //    [ZShare hiddenAnimationView:wSelf.view text:@""];
    }];
}
-(void)parserMoreObject:(id)resultObject tag:(NSString*)tag
{
    if (resultObject==nil) {
        return;
    }
    id more =[resultObject objectForKey:@"data"];
    if (more && [more isKindOfClass:[NSDictionary class]]) {
        NSArray *keyk =[more allKeys];
        if ([keyk count]>0) {
            if ([keyk containsObject:tag]) {
                NSMutableDictionary *dictOld = [[self.brandDictionary objectForKey:tag] mutableCopy];
                NSDictionary* d = [more objectForKey:tag];
                [d enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
                    [dictOld setValue:obj forKey:key];
                }];
                [self.brandDictionary setValue:dictOld forKey:tag];
            }
        }
    }
    [_tbView reloadData];
    
}
-(void)notificationManager
{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(updateLoveList:)
                                                 name:@"updateLoveList"
                                               object:nil];
}
-(void)updateLoveList:(NSNotification*)notification
{
    DLog(@"%@",notification.object);
    self.currentPage = 0;
    [self getCategoryDataWithBid:self.brandId tag:@""];
}
@end

